package top.heliming.springbootes;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import java.io.IOException;

/**
 * description: //TODO
 *
 * @author: heliming
 * @date:2019/05/19 下午 11:03
 */
public class ClientImpl implements Client {
    @Override
    public void createStudentIndex(TransportClient client, String indexName) {
            CreateIndexRequestBuilder cib = client.admin().indices().prepareCreate(indexName);
            XContentBuilder mapping = null;
            try {
                mapping = XContentFactory.jsonBuilder()
                        .startObject()//表示开始设置值
                        .startObject("properties")//设置只定义字段，不传参
                        .startObject("no") //定义字段名
                        .field("type", "text") //设置数据类型
                        .endObject()
                        .startObject("name")
                        .field("type", "text")
                        .endObject()
                        .startObject("addreess")
                        .field("type", "text")
                        .endObject()
                        .startObject("age")
                        .field("type", "integer")
                        .endObject()
                        .startObject("phone")
                        .field("type", "text")
                        .endObject()
                        .startObject("score")
                        .field("type", "integer")
                        .endObject()
                        .endObject()
                        .endObject();
            } catch (IOException e) {
                e.printStackTrace();
            }
            cib.addMapping("student", mapping);
            cib.execute().actionGet();
    }
}
